﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Account_ChangePassword : PageBase
{
    protected void Page_Init(object sender, EventArgs e)
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (CurrentUser != null)
            {
                string newPassword = CryptographicHelper.GetMd5Hash(Convert.ToString(ConfirmNewPassword.Text));
                string currentPassword = CryptographicHelper.GetMd5Hash(Convert.ToString(CurrentPassword.Text));

                if (string.Compare( CurrentUser.Password, currentPassword, true) == 0)
                {
                    DataResponse<bool?> response = UserManager.ChangePassword(CurrentUser, newPassword);

                    changePasswordMessage.Visible = true;

                    if (response != null)
                    {
                        if (string.IsNullOrEmpty(response.ErrorCode))
                        {
                            if (response.Result.HasValue)
                            {
                                if (response.Result.Value)
                                {
                                    changePasswordMessage.Attributes.Add("class", "success");
                                    changePasswordMessage.InnerHtml = String.Format("Your password was changed successfully");
                                }
                                else
                                {
                                    changePasswordMessage.Attributes.Add("class", "error");
                                    changePasswordMessage.InnerHtml = "Password change failed. Please try again";
                                }
                            }
                            else
                            {
                                changePasswordMessage.Attributes.Add("class", "error");
                                changePasswordMessage.InnerHtml = "Password change failed. Please try again";
                            }
                        }
                        else
                        {
                            changePasswordMessage.Attributes.Add("class", "error");
                            changePasswordMessage.InnerHtml = "Password change failed. Please try again";
                        }
                    }
                    else
                    {
                        changePasswordMessage.Attributes.Add("class", "error");
                        changePasswordMessage.InnerHtml = "Password change failed. Please try again";
                    }
                }
                else
                {
                    changePasswordMessage.Attributes.Add("class", "error");
                    changePasswordMessage.InnerHtml = "Invalid current password submitted";
                }
            }
        }
    }

    protected void ValidCurrentPasswordValidator_Validate(object source, ServerValidateEventArgs args)
    {
        bool isValid = false;
        if(!string.IsNullOrEmpty(args.Value))
        {
            string newPassword = CryptographicHelper.GetMd5Hash(Convert.ToString(args.Value));

            if (string.Compare(CurrentUser.Password, newPassword, true) == 0)
            {
                isValid = true;
            }
        }
        args.IsValid = isValid;
    }
}
